<?php
    require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
    require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'check_admin_login.php';
    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'html_generator.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_industry.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_redirect.php';
    $title = 'Admin Panel';
    $success = NULL;
    $error = NULL;

    //GET VARIABLES
    $edit_item['id'] = 0;
    $edit_item['name'] = "";
    $id = filter_input(INPUT_POST, 'hidden_id');
    $name = filter_input(INPUT_POST, 'txt_name');
    $btn_insert = filter_input(INPUT_POST, 'btn_insert');
    $btn_update = filter_input(INPUT_POST, 'btn_update');
    $d_id = filter_input(INPUT_GET, 'd_id');
    $e_id = filter_input(INPUT_GET, 'e_id');

    //INSERT
    if ($btn_insert != NULL && $id == 0) {
        if (api_industry::save(0, $name)) {
            $success = "Insert successfully";
        } else {
            $error = "Insert failed";
        }
    }

    //DELETE
    if ($d_id != NULL) {
        if (api_industry::delete($d_id)) {
            $success = "Delete successfully";
            lib_redirect::Redirect('/admin/industries.php');
        } else {
            $error = "Delete failed";
            lib_redirect::Redirect('/admin/industries.php');
        }
    }

    //GET EDIT ITEM
    if ($e_id != NULL) {
        $edit_item = api_industry::get_by_id($e_id);
    }

    //EDIT
    if ($btn_update != NULL && $id != 0) {
        if (api_industry::save($id, $name)) {
            $success = "Edit successfully";
            lib_redirect::Redirect('/admin/industries.php');
        } else {
            $error = "Edit failed";
        }
    }

?>

<!DOCTYPE html>
<!--[if lt IE 7]>
<html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>
<html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>
<html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->

<html>
<head>
    <?php require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php' ?>
</head>
<body>
<?php require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php' ?>

<!-- Title -->
<div class="section section-black">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="calltoaction-wrapper">
                    <h3>Manage Industry</h3>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- End Title -->


<div class="ts-wrapper">
    <!-- Section Form -->
    <div class="section">
        <div class="container">
            <div class="row">
                <div class="col-md-offset-2 col-md-8">
                    <div class="service-wrapper">
                        <div class="text-center ts-title ts-margin-bottom">Industry Detail</div>
                        <?php show_message($success, $error, 1, 10); ?>
                        <form id="form_insert" name="form_insert" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" class="form-horizontal" role="form">
                            <input name="hidden_id" type="hidden" value="<?php echo $edit_item['id']; ?>">

                            <div class="form-group">
                                <label for="txt_name" class="col-sm-3 control-label">Name <span class="ts-red">*</span></label>

                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="txt_name" id="txt_name" placeholder="Industry Name" required="" value="<?php echo $edit_item['name'] ?>">
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-sm-12">
                                    <input name="btn_insert" value="Insert" type="submit" class="btn btn-default">
                                    <input name="btn_update" value="Update" type="submit" class="btn btn-orange">
                                </div>
                            </div>
                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- End Section Form -->

    <!-- Section Table -->
    <div class="section">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="service-wrapper">
                        <table class="table table-bordered">
                            <tr>
                                <th class="text-center">ID</th>
                                <th class="text-center">Name</th>
                                <th class="text-center" colspan="2">Action</th>
                            </tr>
                            <?php
                                $industries = api_industry::get_all();
                                foreach ($industries as $industry) :
                                    ?>
                                    <tr>
                                        <td><?php echo $industry['id'] ?></td>
                                        <td><?php echo $industry['name'] ?></td>
                                        <td>
                                            <a href="?e_id=<?php echo $industry['id']; ?>">
                                                <img class="ts-action-icon center-block" src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS . 'edit.png' ?>" alt="edit">
                                            </a>
                                        </td>
                                        <td>
                                            <a href="?d_id=<?php echo $industry['id']; ?>" onclick="return confirm('Are you sure?')">
                                                <img class="ts-action-icon center-block" src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS . 'delete.png' ?>" alt="delete">
                                            </a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- End Section Table -->
</div>

<?php require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'footer.php' ?>
</body>
</html>